package com.ifcar.app.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ifcar.app.rss.GetColumnsInfo;
import com.ifcar.app.util.StringUtil;
import com.ifcar.app.util.ThemeSettingsHelper;
import com.ifcar.app.activity.R;

public class SearchResultAdapter extends BaseAdapter{

	private Context mContext;
	private List<Map<String, Object>> mList;
	private LayoutInflater mInflater;
	private ThemeSettingsHelper mThemeSettingsHelper;
	
	public SearchResultAdapter(Context context, ThemeSettingsHelper themeSettingsHelper, List<Map<String, Object>> list){
		mContext = context;
		mList = list;
		mInflater = LayoutInflater.from(context);
		mThemeSettingsHelper = themeSettingsHelper;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if(null == convertView){
			convertView = mInflater.inflate(R.layout.search_result_item, null);
			HolderView holder = new HolderView();
			holder.mSearchTitle = (TextView) convertView.findViewById(R.id.search_result_title);
			holder.mSearchPtime = (TextView) convertView.findViewById(R.id.search_ptime);
			holder.mSplitView = convertView.findViewById(R.id.bottom_split_line);
			convertView.setTag(holder);
		}
		HolderView holder = (HolderView) convertView.getTag();
		
		Map<String, Object> map = (Map<String, Object>) getItem(position);
		String title = (String) map.get(GetColumnsInfo.TITLE_TAG);
		String ptime = (String) map.get(GetColumnsInfo.PTIME_TAG);
		
		holder.mSearchTitle.setText(title);		
		holder.mSearchPtime.setText(StringUtil.getDate(ptime));

		mThemeSettingsHelper.setTextViewColor(mContext, holder.mSearchTitle, R.color.list_title_color);
		mThemeSettingsHelper.setTextViewColor(mContext, holder.mSearchPtime, R.color.list_desc_color);
		mThemeSettingsHelper.setViewBackgroud(mContext, holder.mSplitView, R.drawable.list_divider_line);
		
		return convertView;
	}
	
	class HolderView{
		TextView mSearchTitle;
		TextView mSearchPtime;
		View mSplitView;
	}

}
